﻿function canvasDelegation(canvasId, callback, funName, arg) {
    this.callEnd_state = true; //调用是否结束的状态
    //
    var canvas_context = null;
    var _arg = arg || new Object();
    var draw_end_callback = callback;
    var _this = this;
    var prevImg = null;
    var fun = null;
    //
    var canvas = document.getElementById(canvasId);
    if (canvas.getContext) {
        canvas_context = canvas.getContext("2d");
    } else {
        alert("很遗憾，你的浏览器不支持HTML5");
        return;
    }
    try {
        eval("fun=new " + funName + "(canvas_context,callback,_arg)");
    }
    catch (ex) {
        alert("调用canvas函数出错");
        return;
    }
    this.Draw = function (img) {
        if (!img) {
            return;
        }
        this.callEnd_state = false;
        if (!prevImg) {
            prevImg = img;
            fun.Show(img, _arg);
        }
        else {
            _arg.prevImg = prevImg;
            prevImg = null;
            fun.Hide(img, _arg);
        }
    }
    this.Clear = function () {
        if (prevImg) {
            this.callEnd_state = false;
            _arg.prevImg = prevImg;
            fun.Hide(null, _arg);
        }
    }
    this.SetArg = function (parameters) {
        if (parameters) {
            arg = arg || new Object();
            for (var i in parameters) {
                arg[i] = parameters[i];
            }
        }
    }
    function callback(img, fun, arg) {
        if (prevImg) {
            prevImg = img;
            _this.peopleHead_show_state = false;
            draw_end_callback();
            draw_end_callback = null;
        }
        else {
            prevImg = img;
            fun.Show(img, arg);
        }
    }
}